/**
 * 键盘监听控制
*/
const MOVE_LEFT = GameEvent.KEY.LEFT;
const MOVE_RIGHT = GameEvent.KEY.RIGHT;

// const INPUT_EVENT

cc.macro.ENABLE_TILEDMAP_CULLING = false;

var InputControl = cc.Class({
    extends: cc.Component,
    // use this for initialization
    ctor: function () {
        if (CC_EDITOR) return;
        cc.log("输入控制器初始化.....");
        this.inputsDownPC = {};
        this.inputsUpPC = {};
        this.inputsPressedPC = {};
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);

        this._lastDownKey = 0;
        this._lastUpKey = 0;

        this._vTag = false;

    },

    onKeyDown(event) {

        var keyCode = event.keyCode;
        switch (keyCode) {
            case cc.macro.KEY.left:
                this.inputsDownPC[GameEvent.KEY.LEFT] = true;
                this.inputsDownPC[GameEvent.KEY.RIGHT] = false;
                PlayerStates.setPlayerState(PlayerStates.PlayerStatesEnum.PlayerStartedToMove)
                break;
            case cc.macro.KEY.right:
                this.inputsDownPC[GameEvent.KEY.RIGHT] = true;
                this.inputsDownPC[GameEvent.KEY.LEFT] = false;
                PlayerStates.setPlayerState(PlayerStates.PlayerStatesEnum.PlayerStartedToMove)
                break;
            case cc.macro.KEY.v:
                if (this._vTag) {
                    this.inputsDownPC[GameEvent.KEY.V]  = false;
                    return;
                }
                
                PlayerStates.setPlayerState(PlayerStates.PlayerStatesEnum.PlayerJump)
                this.inputsDownPC[GameEvent.KEY.V] = true;
                this._vTag = true;
                break;
            case cc.macro.KEY.c:
                this.inputsDownPC[GameEvent.KEY.C] = true;
                PlayerStates.setPlayerState(PlayerStates.PlayerStatesEnum.PlayerShoot)
                break;
            case cc.macro.KEY.x:
                this.inputsDownPC[GameEvent.KEY.X] = true;
                PlayerStates.setPlayerState(PlayerStates.PlayerStatesEnum.PlayerXAttack)
                break;
        }
    },

    onKeyUp(event) {

        var keyCode = event.keyCode;
        switch (keyCode) {
            case cc.macro.KEY.left:
                this.inputsDownPC[GameEvent.KEY.LEFT] = false;
                break;
            case cc.macro.KEY.right:
                this.inputsDownPC[GameEvent.KEY.RIGHT] = false;
                break;
            case cc.macro.KEY.v:
                this.inputsDownPC[GameEvent.KEY.V] = false;
                this._vTag = false
                break;
            case cc.macro.KEY.c:
                this.inputsDownPC[GameEvent.KEY.C] = false;
                break;
            case cc.macro.KEY.x:
                this.inputsDownPC[GameEvent.KEY.X] = false;
                break;
        }
    },

    setControl: function (inptNum, bool) {
        this.inputsDownPC[inptNum] = bool
    },

    getControl: function (inptNum) {
        return this.inputsDownPC[inptNum];
    },


    lateUpdateKey: function () {
        // var object = this.inputsDownPC;
        // // if ()
        // for (const key in object) {

        //     if (object.hasOwnProperty(key)) {
        //         // const element = object[key];
        //         object[Number(key)] = false;

        //     }
        // }
        // var object = this.inputsUpPC;
        // cc.log("object2",object)
        // for (const key in object) {
        //     if (object.hasOwnProperty(key)) {
        //         // const element = object[key];
        //         object[Number(key)] = false;

        //     }
        // }
    },
});
var a = new InputControl();
window.InputControl = a;
